import { parse } from 'url';
import {
  TableListItem,
  TableListParams,
  GardenTypeItem,
  PropertyItem,
  AreaListItem,

} from './data.d';

const name = [
  '宏源大厦物业',
  '中国人民大学物业',
  '北京林业大学物业',
  '河北联合大学轻工业学院',
];

function getCampusCommunityList(
  req: { url: any },
  res: {
    json: (
      arg0: { body: { list: TableListItem[]; count: number; } },
    ) => void;
  },
  u: any,
) {
  let new_url = u;
  if (!new_url || Object.prototype.toString.call(new_url) !== '[object String]') {
    new_url = req.url;
  }

  const count = 50;
  const params = (parse(new_url, true).query as unknown) as TableListParams;

  const { pageIndex, pageSize } = params;
  const starNum = (pageIndex - 1) * pageSize;
  const endNum = pageIndex * pageSize;

  const dataSource: TableListItem[] = [];

  [...new Array(endNum - starNum)].map((item, index) => {
    dataSource.push({
      id: starNum + index + 1,
      name: name[Math.floor(Math.random() * 4)],
      mgrPhone: '010-34567890',
      manager: '张三',
      pinyin: '',
      propertyId: 3,
      propertyName: '宏源大厦物业',
      orgId: 58,
      tariffpkgId: 0,
      tariffpkgName: '',
      type: 13402,
      pareaCode: '001',
      careaCode: '001001',
      dareaCode: '001001001',
      ecEnabled: 1,
      letterEnabled: 1,
      longitude: '345.56764',
      latitude: '356.56788',
      openCodeType: 1,
      address: '北京市海淀区清华东路36号',
      createUser: 1,
      createUserName: 'admin',
      createTime: '2010-11-20',
      updateUser: 1,
      updateTime: '2010-11-20',
    });
    return dataSource;
  });

  const result = {
    code: 0,
    body: {
      list: dataSource,
      count,
    },
  };

  return res.json(result);
}

function getGardenType(
  req: { url: any },
  res: {
    json: (
      arg0: { code: number, body: GardenTypeItem },
    ) => void;
  },
  u: any,
) {
  let new_url = u;
  if (!new_url || Object.prototype.toString.call(new_url) !== '[object String]') {
    new_url = req.url;
  }

  const dataSource: GardenTypeItem[] = [
    { mCode: 13401, codeName: '小区' },
    { mCode: 13402, codeName: '学校' },
  ];

  const result: any = {
    code: 0,
    body: dataSource,
  };

  return res.json(result);
}

function getProperty(
  req: { url: any },
  res: {
    json: (
      arg0: { code: number, body: PropertyItem },
    ) => void;
  },
  u: any,
) {
  let new_url = u;
  if (!new_url || Object.prototype.toString.call(new_url) !== '[object String]') {
    new_url = req.url;
  }

  const dataSource: PropertyItem[] = [];
  [...new Array(20)].map((item, index) => (
    dataSource.push({
      id: index + 1,
      name: name[Math.floor(Math.random() * 4)],
      telephone: '13333333333',
      pinyin: 'pinyin',
      address: '北京北京市朝阳区酒仙桥宏源大厦',
      createUserName: '张三丰',
      createTime: '2019-08-20',
    })
  ));

  const result: any = {
    code: 0,
    body: {
      count: 20,
      list: dataSource,
    },
  };

  return res.json(result);
}

function getAreaList(
  req: { url: any },
  res: {
    json: (
      arg0: { code: number, body: AreaListItem },
    ) => void;
  },
  u: any,
) {
  let new_url = u;
  if (!new_url || Object.prototype.toString.call(new_url) !== '[object String]') {
    new_url = req.url;
  }

  const dataSource: AreaListItem[] = [
    {
      id: 1,
      code: '11',
      name: '北京',
      parentCode: '',
      orderNo: 0,
      weatherCode: '101010100',
      desc: '',
    },
    {
      id: 2,
      code: '12',
      name: '天津',
      parentCode: '',
      orderNo: 0,
      weatherCode: '101010100',
      desc: '',
    },
    {
      id: 3,
      code: '13',
      name: '山东',
      parentCode: '',
      orderNo: 0,
      weatherCode: '101010100',
      desc: '',
    },
  ];

  const result: any = {
    code: 0,
    body: dataSource,
  };

  return res.json(result);
}

export default {
  'GET /garden/list': getCampusCommunityList,
  'GET /code/gardenType': getGardenType,
  'GET /property/list': getProperty,
  'GET /area/list': getAreaList,
};
